import { Component } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { HttpService } from '../../service/http.service';

import { NzUploadFile } from 'ng-zorro-antd/upload';

@Component({
  selector: 'app-system',
  templateUrl: './system.component.html',
  styleUrls: ['./system.component.css'],
})
export class SystemComponent {
  constructor(
    private router: Router,
    private httpService: HttpService,
    private modalService: NzMessageService,
  ) {}

  uploading = false;
  fileList: NzUploadFile[] = [];

  beforeUpload = (file: NzUploadFile): boolean => {
    this.fileList = this.fileList.concat(file);
    return false;
  };

  handleUpload(): void {
    this.uploading = true;
  }

  uploadFile() {
    const url = '/rest/system/upload';
    const data = new FormData();

    this.fileList.forEach((file: any) => {
      data.append('file', file);
    });
    this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code == '0000') {
        this.modalService.success('上传成功');
      } else {
        this.modalService.success('上传失败');
      }
    });
  }
}
